package com.sduept.nwld.dataserver.controller.equipment.assessment;

import com.sduept.bigdata.equipment.assessment.model.hiddendanger.knowledgebase.KnowledgeBase;
import com.sduept.bigdata.equipment.assessment.model.hiddendanger.track.HiddenDangerTrack;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionExpert;
import com.sduept.bigdata.equipment.assessment.service.HiddenDangerKnowledageBaseService;
import com.sduept.bigdata.equipment.assessment.service.HiddenDangerTrackService;
import com.sduept.bigdata.equipment.assessment.service.InspectionExpertService;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.core.entity.AbstractStandardDict;
import com.sduept.core.service.StandardDictService;
import com.sduept.core.service.impl.StandardDictConfigurator;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.*;

@Named
@ViewScoped
/**
 * 隐患追踪controller类
 */
public class HiddenDangerTrackDetailController extends AbstractController {
	private static final long serialVersionUID = 5233283549395529450L;
	
	@Autowired
    private HiddenDangerTrackService trackService;
	@Autowired
    private InspectionExpertService expertService;
	@Autowired
	private StandardDictService sman;
	@Autowired
	private StandardDictConfigurator sdc;
	@Autowired
	private HiddenDangerKnowledageBaseService knowledgeService;
	
   private List<String> voltageList = new ArrayList<String>(); // 电压等级
   private List<String> classList = new ArrayList<String>(); // 对应分类
   private boolean status;//编辑时显示可编辑内容，详细时不可编辑
   private String isFinished = "全部"; // 是否完成
   private String selectedVoltage = "全部";//电压等级
   private HiddenDangerTrack currentTrack = null;
   private Map<String, List<String>> discMap = null;//字典map
   private Class<? extends AbstractStandardDict> T = null;	
   private String changeStatus;
   
	@PostConstruct
	public void init() {
		String trackId = getParameter("trackId");
		  changeStatus = getParameter("changeStatus");
		
		initStatus();
		initDics();
		try {
			currentTrack = trackService.findById(trackId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		initAutoCompleteList();
	}
	 
	private void initStatus() {
          if("true".equals(changeStatus)) {
        	  status = true;
          }else {
        	  status = false;
          }
	}

	/**
     * 初始化字典Map
     */
	@SuppressWarnings("unchecked")
	private void initDics() {
		discMap = new HashMap<String, List<String>>();
		List<String> dicts = Arrays.asList("HiddenDangerQuesType","HiddenDangerReasonType","HiddenDangerLevel"	,"HiddenDangerSource");
		try {
			for (String className : dicts) {
				T=sdc.getT(className);
				List<AbstractStandardDict> dictList = (List<AbstractStandardDict>) sman.findAll(Class.forName(T.getName()));
				List<String> values = new ArrayList<String>();
				for (AbstractStandardDict d : dictList) {
					values.add(d.getName());
				}
				discMap.put(className, values);
			}
		} catch (Exception e) {
			addErrorMessage("字典库加载失败");
			e.printStackTrace();
		}
	}
	/**
	 * 更新
	 * @param track
	 * @throws Exception 
	 */
	public void updateTrack() throws Exception {
         if(currentTrack!=null) {
     		try {
				trackService.createOrUpdate(currentTrack);
				 addSuccessMessage("保存成功！");
			} catch (Exception e) {
               addErrorMessage("保存失败！");
				e.printStackTrace();
			}
     		if(StringUtils.isNotBlank(currentTrack.getHiddenDangerDesc())&&StringUtils.isNotBlank(currentTrack.getRectificationPlan())) {
     			createKnowledgeBase();
     		}
     		
         }
        
	}
	
	private void createKnowledgeBase() throws Exception {//专家 等级 描述
		String hiddenDangerDesc  = currentTrack.getHiddenDangerDesc();
		String rectificationPlan = currentTrack.getRectificationPlan(); 
		List<KnowledgeBase> bases = knowledgeService.findKnowledgeBasesByHiddenDangeDescKey(hiddenDangerDesc);//查询一条是否为空，是 就增加  否 判断整改方案是否相同，否就增加，替换findAll()
		for (KnowledgeBase kb : bases) {
			if(hiddenDangerDesc.equals(kb.getHiddenDangerDesc())&&rectificationPlan.equals(kb.getRectificationPlan())) {
				return;
			}
		}
		String expertName =currentTrack.getInspectionExpert();
		InspectionExpert expert = expertService.findByName(expertName);
	    try {
			KnowledgeBase base = new KnowledgeBase();
			base.setCreatedTime(new Date());
			base.setExpertName(expertName);
			base.setExpertCode(expert.getCode());
			base.setHiddenDangerDesc(hiddenDangerDesc);
			base.setHiddenDangerGrade(currentTrack.getHiddenDangerGrade());//隐患等级
			base.setRectificationPlan(rectificationPlan);//整改计划
			base = knowledgeService.createOrUpdateKnowledgeBase(base);
		} catch (Exception e) {
			e.printStackTrace();
		}
	    
	}
	
	
	
	
	/**
	 * 初始化自动填充列表
	 */
	private void initAutoCompleteList() {
		classList = Arrays.asList("一", "二", "三", "四", "五", "六");
		voltageList = Arrays.asList("35kV", "110kV", "220kV", "500kV", "750kV", "800kV", "1000kV");
	}
	
	
	/**
	 * 电压等级
	 * @param query
	 * @return
	 */
	public List<String> completeVoltage(String query) {
		List<String> results = new ArrayList<String>();
		for (String s : voltageList) {
			if (s.contains(query)) {
				results.add(s);
			}
		}
		return results;
	}
	
	/**
	 * 对应分类
	 * @param query
	 * @return
	 */
	public List<String> completeClassify(String query) {
		List<String> results = new ArrayList<String>();
		for (String s : classList) {
			if (s.contains(query)) {
				results.add(s);
			}
		}
		return results;
	}
	
	 /**
		 * 隐患来源
		 * @param query
		 * @return
		 */
	public List<String> completeHiddenDangerSources(String query) {
		List<String> results = new ArrayList<String>();
		for (String s : discMap.get("HiddenDangerSource")) {
			if (s.contains(query)) {
				results.add(s);
			}
		}
		return results;
	}



	
	/**
	 * 改变编辑和查看详细的显示状态
	 */
//	public void preEdit() {
//		status = true;
//		
//	}
//	
	
	public void changeStatus() {
		status = false;
	}
	public boolean isStatus() {
		return status;
	}
	public void setStatus(boolean status) {
		this.status = status;
	}
	public String getIsFinished() {
		return isFinished;
	}
	public void setIsFinished(String isFinished) {
		this.isFinished = isFinished;
	}
	public String getSelectedVoltage() {
		return selectedVoltage;
	}
	public void setSelectedVoltage(String selectedVoltage) {
		this.selectedVoltage = selectedVoltage;
	}
	public HiddenDangerTrack getCurrentTrack() {
		return currentTrack;
	}
	public void setCurrentTrack(HiddenDangerTrack currentTrack) {
		this.currentTrack = currentTrack;
	}

	public String getChangeStatus() {
		return changeStatus;
	}

	public void setChangeStatus(String changeStatus) {
		this.changeStatus = changeStatus;
	}

	

}
